package com.ovo.rxandroiddemo.utils;

import android.widget.Toast;

import com.ovo.rxandroiddemo.app.BaseApplication;

public class ToastUtils {

    private static Toast toast;
    private static long oneTime1 = 0;
    private static long twoTime1 = 0;
    private static long oneTime2 = 0;
    private static long twoTime2 = 0;
    private static int info = -1;
    private static String textOld = "";

    /***显示*/
    public static void show(int text) {
        show(text, Toast.LENGTH_SHORT);
    }

    public static void show(int text, int duration) {
        textOld = "";
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.context, text, duration);
            toast.show();
            oneTime1 = System.currentTimeMillis();
        } else {
            twoTime1 = System.currentTimeMillis();
            if (info == text) {
                if (twoTime1 - oneTime1 > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                info = text;
                toast.setText(text);
                toast.show();
            }
        }
        oneTime1 = twoTime1;
    }

    /***显示*/
    public static void showMsg(String text) {
        showMsg(text, Toast.LENGTH_SHORT);
    }

    public static void showMsg(String text, int duration) {
        info = -1;
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.context, text, duration);
            toast.show();
            oneTime2 = System.currentTimeMillis();
        } else {
            twoTime2 = System.currentTimeMillis();
            if (textOld.equals(text)) {
                if (twoTime2 - oneTime2 > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                textOld = text;
                toast.setText(text);
                toast.show();
            }
        }
        oneTime2 = twoTime2;
    }

}
